package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import model.Tpayment;
import model.Treceipt;
import model.Tsalereturn;
import model.Tsalereturnitem;
import model.Tshoplist;
import model.Tvip;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.PaymentDaoI;
import dao.ReceiptDaoI;
import dao.SaleReturnDaoI;
import dao.SaleReturnItemDaoI;
import dao.ShopListDaoI;
import dao.VipDaoI;
import pageModel.DataGrid;
import pageModel.SaleReturnItem;
import service.SaleReturnItemServiceI;
@Service("saleReturnItemService")
public class SaleReturnItemServiceImpl implements SaleReturnItemServiceI{
private SaleReturnDaoI saleReturnDao;
private SaleReturnItemDaoI saleReturnItemDao;
private ShopListDaoI shopListDao;
private ReceiptDaoI receiptDao;
private PaymentDaoI paymentDao;
private VipDaoI vipDao;
	public SaleReturnDaoI getSaleReturnDao() {
	return saleReturnDao;
}
@Autowired
public void setSaleReturnDao(SaleReturnDaoI saleReturnDao) {
	this.saleReturnDao = saleReturnDao;
}

public SaleReturnItemDaoI getSaleReturnItemDao() {
	return saleReturnItemDao;
}
@Autowired
public void setSaleReturnItemDao(SaleReturnItemDaoI saleReturnItemDao) {
	this.saleReturnItemDao = saleReturnItemDao;
}

	public ShopListDaoI getShopListDao() {
	return shopListDao;
}
@Autowired
public void setShopListDao(ShopListDaoI shopListDao) {
	this.shopListDao = shopListDao;
}

	public ReceiptDaoI getReceiptDao() {
	return receiptDao;
}
@Autowired
public void setReceiptDao(ReceiptDaoI receiptDao) {
	this.receiptDao = receiptDao;
}

	public PaymentDaoI getPaymentDao() {
	return paymentDao;
}
@Autowired
public void setPaymentDao(PaymentDaoI paymentDao) {
	this.paymentDao = paymentDao;
}
	public VipDaoI getVipDao() {
	return vipDao;
}
@Autowired
public void setVipDao(VipDaoI vipDao) {
	this.vipDao = vipDao;
}
	@Override
	public void save(SaleReturnItem saleReturnItem) {
	Tsalereturnitem t = new Tsalereturnitem();
	BeanUtils.copyProperties(saleReturnItem, t);
	t.setId(UUID.randomUUID().toString());
	if(saleReturnItem.getSalereturnno()!=null&&!saleReturnItem.getSalereturnno().trim().equals("")){
    	String hql = "from Tsalereturn t where t.salereturnno=:salereturnno";
		Map<String,Object>params=new HashMap<String,Object>();
		params.put("salereturnno", saleReturnItem.getSalereturnno());
		Tsalereturn ts = saleReturnDao.get(hql, params);
		if(ts!=null){
			t.setTsalereturn(ts);
		}	
    }
	if(saleReturnItem.getTid()!=null&&!saleReturnItem.getTid().trim().equals("")&&saleReturnItem.getName()!=null&&!saleReturnItem.getName().trim().equals("")){
		String hql="from Tshoplist t where t.treceipt.tid=:tid and t.tgood.name=:name";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("tid", saleReturnItem.getTid());
		params.put("name", saleReturnItem.getName());
		Tshoplist ts = shopListDao.get(hql, params);
		if(ts!=null){
			t.setTshoplist(ts);
		}
			saleReturnItemDao.save(t);
			BeanUtils.copyProperties(t,saleReturnItem);
			ts.setAmount(ts.getAmount()-saleReturnItem.getQuantity());
			ts.setPrice(ts.getPrice()-saleReturnItem.getQuantity()*saleReturnItem.getPrice());
			shopListDao.save(ts);
	}
	if(saleReturnItem.getTid()!=null&&!saleReturnItem.getTid().trim().equals("")){
		String hql="from Tpayment t where t.treceipt.tid=:tid";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("tid", saleReturnItem.getTid());
		Tpayment tp = paymentDao.get(hql, params);
		tp.setTotalprice(tp.getTotalprice()-saleReturnItem.getRefundment());
		tp.setChanges(tp.getChanges()+saleReturnItem.getRefundment());
		paymentDao.save(tp);
		String hql1="from Treceipt t where t.tid=:tid";
		Map<String,Object> params1=new HashMap<String,Object>();
		params1.put("tid", saleReturnItem.getTid());
		Treceipt tr = receiptDao.get(hql1, params1);
		tr.setTotalprice(tr.getTotalprice()-saleReturnItem.getRefundment());
		receiptDao.save(tr);
	}
		if(saleReturnItem.getVip()!=null&&!saleReturnItem.getVip().trim().equals("")){
			Tvip v =vipDao.get(Tvip.class,saleReturnItem.getVip());
			v.setIntegration(v.getIntegration()-saleReturnItem.getRefundment());
			vipDao.save(v);
		}
  }
	@Override
	public DataGrid datagrid(SaleReturnItem saleReturnItem) {
		DataGrid dg=new DataGrid();
		String hql="from Tsalereturnitem t where 1=1";
		Map<String,Object> params=new HashMap<String,Object>();
		hql = addWhere(saleReturnItem, hql, params);
		String totalHql="select count(*) "+hql;
		if(saleReturnItem.getSort()!=null){
		hql+=" order by "+saleReturnItem.getSort()+" "+saleReturnItem.getOrder();
		}
		List<Tsalereturnitem> ts=saleReturnItemDao.find(hql,params,saleReturnItem.getPage(),saleReturnItem.getRows());
		List<SaleReturnItem> l=new ArrayList<SaleReturnItem>();
		if(ts!=null&&ts.size()>0){
			for(Tsalereturnitem t :ts){
				SaleReturnItem s=new SaleReturnItem();
				BeanUtils.copyProperties(t, s);
				if(t.getTsalereturn()!=null){
					s.setSalereturnno(t.getTsalereturn().getSalereturnno());
					s.setReason(t.getTsalereturn().getReason());
					s.setRemark(t.getTsalereturn().getRemark());
					s.setCreatedatetime(t.getTsalereturn().getCreatedatetime());
				}
				if(t.getTshoplist()!=null){
					s.setName(t.getTshoplist().getTgood().getName());
				}
				l.add(s);
			}
		}
		dg.setTotal(saleReturnItemDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
	private String addWhere(SaleReturnItem saleReturnItem, String hql, Map<String, Object> params) {
		if(saleReturnItem.getSalereturnno()!=null&&!saleReturnItem.getSalereturnno().trim().equals("")){
		hql+=" and t.tsalereturn.salereturnno like :salereturnno";
		params.put("salereturnno", "%%"+saleReturnItem.getSalereturnno()+"%%");
		}
		return hql;
	}
}
